package com.example.zg.androidnewview.adapter;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * 创建日期:2019/1/17 on 9:30
 * 描述：
 * 作者: zhougan
 */
public abstract class BaseHFRecyclerViewAdapter<T>  extends RecyclerView.Adapter<BaseHFRecyclerViewAdapter.VH> {
    private List<T> mData;//数据
    //item点击事件接口
    private BaseHFRecyclerViewAdapter.OnItemListener mOnItemListener;
    private View mHeader;
    private View mFooter;
    public static final int  ITEM_TYPE_HEADER=0;//header
    public static final int  ITEM_TYPE_FOOTER=1;//footer
    public static final int  ITEM_TYPE_NOMAL=2;//item


    public BaseHFRecyclerViewAdapter() {

    }

    public BaseHFRecyclerViewAdapter(List<T> data) {
        this.mData = data;
    }

    public List<T> getData() {
        return mData;
    }

    public void setData(List<T> data) {
        this.mData = data;
    }

    public void setHeader(View view){
        mHeader=view;
    }
    public void setFooter(View view){
        mFooter=view;
    }
    public void setOnItemListener(BaseHFRecyclerViewAdapter.OnItemListener onItemListener){
        this.mOnItemListener=onItemListener;
    }


    @Override
    public int getItemCount() {
        int itemCount=0;
        if(mData!=null){
            itemCount= mData.size();
        }
        if(mHeader!=null){
            itemCount++;
        }
        if(mFooter!=null){
            itemCount++;
        }
        return itemCount;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0 && mHeader!=null){
            return ITEM_TYPE_HEADER;
        }else if(position==getItemCount()-1 && mFooter!=null){
            return ITEM_TYPE_FOOTER;
        }else{
            return ITEM_TYPE_NOMAL;

        }
    }

    public T getItemData(int position){
        int itemViewType = getItemViewType(position);
        //Header或footer
        if(itemViewType==ITEM_TYPE_HEADER ||itemViewType==ITEM_TYPE_FOOTER){
            return null;
        }else{
            if(mHeader!=null){
                return  mData.get(position-1);
            }else{
                return  mData.get(position);
            }
        }
    }



    @NonNull
    @Override
    public BaseHFRecyclerViewAdapter.VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==ITEM_TYPE_HEADER){
            return new BaseHFRecyclerViewAdapter.VH(mHeader);
        }else if(viewType==ITEM_TYPE_FOOTER){
            return new BaseHFRecyclerViewAdapter.VH(mFooter);
        }else {
            return new BaseHFRecyclerViewAdapter.VH(LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false),mOnItemListener);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull BaseHFRecyclerViewAdapter.VH holder, int position) {
        if(getItemViewType(position)==ITEM_TYPE_NOMAL){
            onBindData(holder,getItemData(position),position,getItemViewType(position));
        }
    }





    /**
     * 返回布局文件的id
     * @param viewType
     * @return
     */
    public abstract int getLayoutId(int viewType);

    /**
     * 绑定数据到item
     * @param holder
     * @param data
     * @param position
     * @param viewType
     */
    public abstract void onBindData(BaseHFRecyclerViewAdapter.VH holder, T data, int position, int viewType);




    //自定义ViewHolder
    public static class VH extends RecyclerView.ViewHolder{
        private SparseArray<View> mViews;//存储布局中的字组件item.findViewById集合
        private BaseHFRecyclerViewAdapter.OnItemListener mOnItemListener;

        public VH(View itemView) {
            super(itemView);
            mViews=new SparseArray<>();
        }

        private VH(View itemView, BaseHFRecyclerViewAdapter.OnItemListener onItemListener) {
            super(itemView);
            mViews=new SparseArray<>();
            this.mOnItemListener=onItemListener;
            if(mOnItemListener!=null){
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //getLayoutPosition是RecyclerView定义的获取item位子的方法
                        mOnItemListener.onItemClickListener(v,getLayoutPosition());
                    }
                });
                itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        return mOnItemListener.onItemLongClickListener(v,getLayoutPosition());
                    }
                });
            }
        }

        /**
         * 根据id获取布局文件中的子组件
         * @param id
         * @param <T>
         * @return
         */
        public <T extends View> T getView(@IdRes int id){
            View view=mViews.get(id);
            if(view==null){
                view=itemView.findViewById(id);
                mViews.put(id,view);
            }
            return (T)view;
        }

        /**
         * 设置文字比较常用,所以单独定义出来
         * @param id
         * @param value
         */
        public void setText(int id,String value){
            TextView textView=getView(id);
            textView.setText(value);
        }
    }

    /**
     * item点击接口
     */
    public static interface OnItemListener{
        void onItemClickListener(View view,int position);
        boolean onItemLongClickListener(View view,int position);
    }
}
